package com.tianyao.compose

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.tianyao.compose.baseuse.activity.animate.AnimatedActivity
import com.tianyao.compose.baseuse.activity.animate.AnimatedContentActivity
import com.tianyao.compose.baseuse.activity.animate.AnimatedCustomActivity
import com.tianyao.compose.baseuse.activity.animate.AnimatedLowGradeApi2Activity
import com.tianyao.compose.baseuse.activity.animate.AnimatedLowGradeApi3Activity
import com.tianyao.compose.baseuse.activity.animate.AnimatedLowGradeApiActivity
import com.tianyao.compose.baseuse.activity.animate.AnimatedModifierActivity
import com.tianyao.compose.baseuse.activity.animate.AnimatedSizeActivity
import com.tianyao.compose.baseuse.bean.JumpParamsBean
import com.tianyao.compose.ui.theme.ComposeProTheme

class MainActivity : ComponentActivity() {

    //跳转的数据
    private val mJumpData = mutableListOf<JumpParamsBean>().apply {
        add(JumpParamsBean(AnimatedActivity::class.java, "跳转到AnimatedActivity"))
        add(JumpParamsBean(AnimatedModifierActivity::class.java, "跳转到AnimatedModifierActivity"))
        add(JumpParamsBean(AnimatedCustomActivity::class.java, "跳转到AnimatedCustomActivity"))
        add(JumpParamsBean(AnimatedContentActivity::class.java, "跳转到AnimatedContentActivity"))
        add(JumpParamsBean(AnimatedSizeActivity::class.java, "跳转到AnimatedSizeActivity"))
        add(JumpParamsBean(AnimatedLowGradeApiActivity::class.java, "跳转到AnimatedLowGradeApiActivity"))
        add(JumpParamsBean(AnimatedLowGradeApi2Activity::class.java, "跳转到AnimatedLowGradeApi2Activity"))
        add(JumpParamsBean(AnimatedLowGradeApi3Activity::class.java, "跳转到AnimatedLowGradeApi3Activity"))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeProTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("Android")
                }
            }
        }
    }


    @Composable
    fun Greeting(name: String, modifier: Modifier = Modifier) {
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()                   //整个屏幕
                .background(Color.LightGray),    //背景浅灰色
            contentPadding = PaddingValues(15.dp),   //内边距
            verticalArrangement = Arrangement.spacedBy(15.dp)  //条目间竖直方向的间距
        ) {
            //LazyListScope的扩展函数,可以获取到index
            itemsIndexed(items = mJumpData, key = { index, _ ->
                index
            }) { _, jumpParamsBean ->
                Box(
                    modifier = Modifier
                        .fillMaxWidth()      //最大宽度
                        .height(50.dp)
                        .background(Color.Cyan),   //条目青色
                    contentAlignment = Alignment.Center
                ) {
                    ClickableText(text = AnnotatedString(jumpParamsBean.buttonText), onClick = {
                        startActivity(Intent(this@MainActivity, jumpParamsBean.clazz))
                    })
                }
            }
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun GreetingPreview() {
        ComposeProTheme {
            Greeting("Android")
        }
    }

}